package com.cn.goodsadmin.controller;

import com.cn.goodsadmin.common.PageQueryResult;
import com.cn.goodsadmin.dao.DestDTO;
import com.cn.goodsadmin.po.Logistics;
import com.cn.goodsadmin.po.PanicLog;
import com.cn.goodsadmin.po.SendPanicOrderTemplate;
import com.cn.goodsadmin.service.LogisticsService;
import com.cn.goodsadmin.service.PanicLogService;
import com.cn.goodsadmin.service.UserAddressService;
import com.cn.goodsadmin.util.ErrorMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
public class PanicController {

    @Autowired
    PanicLogService panicLogService;

    @Autowired
    UserAddressService userAddressService;

    @Autowired
    LogisticsService logisticsService;


    /**
     * @param pageindex
     * @param pagesize
     * @return
     */
    //@LoginRequired
    @RequestMapping(value = "/getPanicInfo", method = RequestMethod.GET)
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap getPanicInfo(
            @RequestParam(value = "pageindex")int pageindex,
            @RequestParam(value = "pagesize") int pagesize,
            @RequestParam(value = "appid")    int appid,
            @RequestParam(value = "type" ,required = false)Integer type,
            @RequestParam(value = "state")    int state)
    {
        SupplyMap supplyMap = new SupplyMap();
        try {
            PageQueryResult<DestDTO> pageQueryResult=panicLogService.findAllPanicLogPage(pageindex,pagesize,type,appid,state);
            List<DestDTO> destDTOS=pageQueryResult.getQueryResult();
            if(destDTOS!=null&&destDTOS.size()>0) {
                List<SendPanicOrderTemplate>  templates=new ArrayList<SendPanicOrderTemplate>();
                for(DestDTO destDTO:destDTOS){
                    SendPanicOrderTemplate template=new SendPanicOrderTemplate();
                    PanicLog panicLog=destDTO.getPanicLog();
                    template.panicLog=panicLog;
                    int addrid=panicLog.getcAddrid();
                    template.useraddress=userAddressService.findUserAddress(addrid);
                    String logid=panicLog.getcLogisticsid();
                    if(logid!=null) {
                        Logistics logistics = logisticsService.findLogisticsByLid(logid);
                        template.logistics=logistics;
                    }
                    templates.add(template);
                }
                supplyMap.put("templates", templates);
            }
            supplyMap.put("pagecount",pageQueryResult.getPageCount());
            supplyMap.put("pageIndex",pageQueryResult.getPageIndex());
            supplyMap.put("pagesize",pageQueryResult.getPageSize());
            supplyMap.put("totalcount",pageQueryResult.getTotalCount());
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }


    @LoginRequired
    @RequestMapping(value = "/sendPanicOrder", method = RequestMethod.GET,params = {"pid","appid","logisticsid","logisticsname"})
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap updatePanicStutas(String pid,int appid,String logisticsid,String logisticsname){
        SupplyMap supplyMap=new SupplyMap();
        try{
            panicLogService.sendPanicOrder(pid,appid,logisticsid,logisticsname);
        }catch (Exception e){
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }
}
